package com.share.common.core.context;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.alibaba.ttl.TransmittableThreadLocal;
import com.share.common.core.constant.SecurityConstants;
import com.share.common.core.text.Convert;
import com.share.common.core.utils.StringUtils;

/**
 * ThreadLocal类
 * 获取当前线程变量中的 用户id、用户名称、Token等信息
 * 注意： 必须在网关通过请求头的方法传入，在Interceptor拦截器设置值。
 *
 */
public class SecurityContextHolder {
    //ThreadLocal对象 使用的阿里开源的扩展 ThreadLocal 内部ThreadLocalMap 存储的value为一个map，key为对ThreadLocal的弱引用
    private static final TransmittableThreadLocal<Map<String, Object>> THREAD_LOCAL = new TransmittableThreadLocal<Map<String, Object>>();

//    public static final ThreadLocal<Map<String,Object>> THREAD_LOCAL = new ThreadLocal<>();

    // set存储的 map
    public static void set(String key, Object value) {
        Map<String, Object> map = getLocalMap();
        map.put(key, value == null ? StringUtils.EMPTY : value);
    }

    // 从threadlocal中存储的map获取指定key的value
    public static String get(String key) {
        Map<String, Object> map = getLocalMap();
        return Convert.toStr(map.getOrDefault(key, StringUtils.EMPTY));
    }

    //从threadlocal中存储的map获取指定key的value,并转换为指定类型
    public static <T> T get(String key, Class<T> clazz) {
        Map<String, Object> map = getLocalMap();
        //获取指定key的值
        Object value = map.getOrDefault(key, null);
        //将值的类型强转为指定类型
        return StringUtils.cast(value);
    }

    // 获取当前线程存储的map对象。如果map为null，初始化为ConcurrentHashMap
    public static Map<String, Object> getLocalMap() {
        Map<String, Object> map = THREAD_LOCAL.get();
        if (map == null) {
            map = new ConcurrentHashMap<String, Object>();
            THREAD_LOCAL.set(map);
        }
        return map;
    }

    public static void setLocalMap(Map<String, Object> threadLocalMap) {
        THREAD_LOCAL.set(threadLocalMap);
    }

    //获取当前线程中的用户 的id
    public static Long getUserId() {
        String userId = get(SecurityConstants.DETAILS_USER_ID);
        //转换为Long,如果无id，默认为0
        Long id = Convert.toLong(userId, 0L);
        return id;
    }

    //设置当前线程的用户 ID
    public static void setUserId(String userId) {
        set(SecurityConstants.DETAILS_USER_ID, userId);
    }

    //获取用户名
    public static String getUserName() {
        return get(SecurityConstants.DETAILS_USERNAME);
    }

    //设置用户名
    public static void setUserName(String username) {
        set(SecurityConstants.DETAILS_USERNAME, username);
    }

    //获取和设置当前线程的用户唯一标识 uuid
    public static String getUserKey() {
        return get(SecurityConstants.USER_KEY);
    }

    public static void setUserKey(String userKey) {
        set(SecurityConstants.USER_KEY, userKey);
    }

    //获取和设置当前线程的权限信息。
    public static String getPermission() {
        return get(SecurityConstants.ROLE_PERMISSION);
    }

    public static void setPermission(String permissions) {
        set(SecurityConstants.ROLE_PERMISSION, permissions);
    }

    //清除当前线程的上下文信息，避免OOM。
    public static void remove() {
        THREAD_LOCAL.remove();
    }
}
